﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QL_NOI_TRU.businessLayers;
using QL_NOI_TRU.Entities;
using System.Data.SqlClient;
using System.IO;

namespace QL_NOI_TRU
{
    public partial class frmThemTTNhanVien : DevExpress.XtraEditors.XtraForm
    {
        private int _maNV = 0;
        NhanVienBLL bll = new NhanVienBLL();


        public frmThemTTNhanVien()
        {
            InitializeComponent();
        }
        private void frmThemTTNhanVien_Load(object sender, EventArgs e)
        {
            var makhoa = "select MA_KHOA,TEN_KHOA from KHOA";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = makhoa;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbKhoa.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_KHOA"], dr["TEN_KHOA"]));
            }
        }

        
        public frmThemTTNhanVien(int manv)
        {
            InitializeComponent();
            this._maNV = manv;

            NhanVien bk = bll.GetNhanVienByID(manv);

            tbmaNV.Text = bk.MA_NV.ToString();
            cbbKhoa.Text = bk.MA_KHOA;
            cbbPhong.Text = bk.MAPH;
            tbHotenNV.Text = bk.HOTEN;
            tbsoCMND.Text = bk.SOCMND;
            cbbGTNV.Text = bk.GIOITINH;
            cbbngaysinhNV.Text = bk.NGAYSINH.ToString();
            tbDiachiNV.Text = bk.DIACHI;
            tbSoDTNV.Text = bk.SODIENTHOAI;
            tbChucDanh.Text = bk.CHUCDANH;
            tbChucVu.Text = bk.CHUCVU;
        }

        private void Reset()
        {
            tbmaNV.Text = "";
            cbbKhoa.SelectedIndex = -1;
            cbbPhong.SelectedIndex = -1;
            tbHotenNV.Text = "";
            tbsoCMND.Text = "";
            cbbGTNV.Text = "";
            cbbngaysinhNV.Text = "";
            tbDiachiNV.Text = "";
            tbSoDTNV.Text = "";
            tbChucDanh.Text = "";
            tbChucVu.Text = "";
        }

        private void btThemNV_Click(object sender, EventArgs e)
        {
            if (_maNV == 0)
            {
                NhanVien bk = new NhanVien(cbbKhoa.Text, cbbPhong.Text, tbHotenNV.Text, tbsoCMND.Text, cbbGTNV.Text.ToString(), cbbngaysinhNV.Text.ToString(), tbDiachiNV.Text, tbSoDTNV.Text, tbChucDanh.Text, tbChucVu.Text);
                if (bll.Insert(bk))
                {
                    MessageBox.Show("Thêm nhân viên mới thành công!");
                    this.Close();
                }
                else MessageBox.Show("Có lỗi xảy ra!");
            }
            else
            {
                NhanVien bk = new NhanVien(_maNV, cbbKhoa.Text, cbbPhong.Text, tbHotenNV.Text, tbsoCMND.Text, cbbGTNV.Text.ToString(), cbbngaysinhNV.Text.ToString(), tbDiachiNV.Text, tbSoDTNV.Text, tbChucDanh.Text, tbChucVu.Text);
                if (bll.Update(bk))
                {
                    MessageBox.Show("Sửa đổi thông tin nhân viên thành công!");
                    this.Close();
                }
                else MessageBox.Show("Có lỗi xảy ra!");
            }
        }

        private void cbbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a = cbbKhoa.SelectedItem.ToString().Substring(0, 2).Trim();
            var maP = String.Format("select * from PHONG_TRUC where MA_KHOA = '{0}'", a);
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maP;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbPhong.Properties.Items.Add(String.Format("{0} | {1} | {2}", dr["MAPH"], dr["MA_KHOA"], dr["TEN_PHONG"]));
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            QL_NOI_TRU.DataLayers.KetNoiHelper hp = new DataLayers.KetNoiHelper();
            OpenFileDialog dlg = new OpenFileDialog();
         //   dlg.Filter = "JPG Files(*.JPG)|*.JPG Files(*.GIF)|*.GIF";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    hp.StorePicture(dlg.FileName);
              //     pictureBox1.Image = Image.FromFile(dlg.FileName);
                   }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            QL_NOI_TRU.DataLayers.KetNoiHelper hp = new DataLayers.KetNoiHelper();
            byte[] img = hp.RetrieveImage();
            MemoryStream str = new MemoryStream(img);
            pictureBox1.Image = Image.FromStream(str);
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void labelControl12_Click(object sender, EventArgs e)
        {

        }

        private void cbbngaysinhNV_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void cbbPhong_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbbGTNV_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btLayma_Click(object sender, EventArgs e)
        {

        }

        private void tbsoCMND_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void tbHotenNV_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void tbSoDTNV_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void tbDiachiNV_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void tbChucVu_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void tbChucDanh_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void tbmaNV_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void labelControl6_Click(object sender, EventArgs e)
        {

        }

        private void labelControl10_Click(object sender, EventArgs e)
        {

        }

        private void labelControl9_Click(object sender, EventArgs e)
        {

        }

        private void labelControl8_Click(object sender, EventArgs e)
        {

        }

        private void labelControl7_Click(object sender, EventArgs e)
        {

        }

        private void labelControl5_Click(object sender, EventArgs e)
        {

        }

        private void labelControl11_Click(object sender, EventArgs e)
        {

        }

        private void labelControl4_Click(object sender, EventArgs e)
        {

        }

        private void labelControl2_Click(object sender, EventArgs e)
        {

        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }
    }
}